﻿using System;
using System.Collections.Generic;
using System.Text;

namespace DBSourceToolsLib
{
    public class FileUtils
    {
        // TODO : COMPLETE 1.2.0.8 Ensure files are read-write 
        public void EnsureFileDirectoryExists(string outputFileName)
        {
            string strBasePath = outputFileName.Substring(0, outputFileName.LastIndexOf("\\") + 1);
            if (!System.IO.Directory.Exists(strBasePath))
                System.IO.Directory.CreateDirectory(strBasePath);
        }

        public static void EnsureFileIsWriteable(string strOutputFileName)
        {
            if (System.IO.File.Exists(strOutputFileName))
            {
                System.IO.FileInfo oFileInfo = new System.IO.FileInfo(strOutputFileName);
                oFileInfo.IsReadOnly = false;
            }
            
        }

        public void EnsureDirectoryExists(string outputDirName)
        {
            if (!System.IO.Directory.Exists(outputDirName))
                System.IO.Directory.CreateDirectory(outputDirName);
        }

        public void DeleteDirRecursive(string strDir)
        {
            try
            {
                if (System.IO.Directory.Exists(strDir))
                {
                    System.IO.DirectoryInfo baseDirInfo = new System.IO.DirectoryInfo(strDir);
                    if (!((baseDirInfo.Attributes & System.IO.FileAttributes.Hidden) == System.IO.FileAttributes.Hidden))
                    {
                        foreach (System.IO.DirectoryInfo oDir in baseDirInfo.GetDirectories())
                        {
                            DeleteDirRecursive(oDir.FullName);
                            //if (!((oDir.Attributes & System.IO.FileAttributes.Hidden) == System.IO.FileAttributes.Hidden))
                            //oDir.Delete(true);
                        }
                        foreach (System.IO.FileInfo oFile in baseDirInfo.GetFiles())
                        {
                            System.IO.File.Delete(oFile.FullName);
                        }
                        System.IO.Directory.Delete(strDir);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("cannot delete " + strDir + " " + ex.ToString());
            }
        }

        public void DeleteFilesInDirRecursive(string strDir)
        {
            try
            {
                if (System.IO.Directory.Exists(strDir))
                {
                    System.IO.DirectoryInfo baseDirInfo = new System.IO.DirectoryInfo(strDir);
                    if (!((baseDirInfo.Attributes & System.IO.FileAttributes.Hidden) == System.IO.FileAttributes.Hidden))
                    {
                        foreach (System.IO.DirectoryInfo oDir in baseDirInfo.GetDirectories())
                        {
                            DeleteFilesInDirRecursive(oDir.FullName);
                        }
                        foreach (System.IO.FileInfo oFile in baseDirInfo.GetFiles())
                        {
                            System.IO.File.Delete(oFile.FullName);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("cannot delete " + strDir + " " + ex.ToString());
            }
        }



        public void DeleteFilesInDir(string strDir)
        {
            if (System.IO.Directory.Exists(strDir))
            {
                System.IO.DirectoryInfo baseDirInfo = new System.IO.DirectoryInfo(strDir);
                foreach (System.IO.FileInfo oFile in baseDirInfo.GetFiles())
                {
                    EnsureFileIsWriteable(oFile.FullName);
                    System.IO.File.Delete(oFile.FullName);
                }
            }
        }

        public void CopyFilesRecursive(string strSourceDir, string strTargetDir)
        {
            EnsureDirectoryExists(strTargetDir);
            if (System.IO.Directory.Exists(strSourceDir))
            {
                System.IO.DirectoryInfo baseDirInfo = new System.IO.DirectoryInfo(strSourceDir);
                foreach (System.IO.DirectoryInfo oDir in baseDirInfo.GetDirectories())
                {
                    //System.IO.FileAttributes oAttrib = oDir.Attributes;
                    //System.IO.FileAttributes oAttribCompare = oDir.Attributes & System.IO.FileAttributes.Hidden;
                    //oAttrib = oAttrib & System.IO.FileAttributes.Hidden;

                    if (!((oDir.Attributes & System.IO.FileAttributes.Hidden) == System.IO.FileAttributes.Hidden))
                        CopyFilesRecursive(oDir.FullName, strTargetDir +"\\" + oDir.Name);
                }
                foreach (System.IO.FileInfo oFile in baseDirInfo.GetFiles())
                {
                    System.IO.File.Copy(oFile.FullName, strTargetDir + "\\" + oFile.Name);
                }
            }

        }


        internal void DeleteFile(string m_strZipFileName)
        {
            if (System.IO.File.Exists(m_strZipFileName))
                System.IO.File.Delete(m_strZipFileName);
        }

        public int CountFilesInDir(string strDir)
        {
            int iFileCount = 0;

            System.IO.DirectoryInfo baseDirInfo = new System.IO.DirectoryInfo(strDir);
            foreach (System.IO.DirectoryInfo oDir in baseDirInfo.GetDirectories())
            {
                // zip files do not exclude hiddent directories
                //if (!((oDir.Attributes & System.IO.FileAttributes.Hidden) == System.IO.FileAttributes.Hidden))
                    iFileCount += CountFilesInDir(oDir.FullName);
            }
            foreach (System.IO.FileInfo oFile in baseDirInfo.GetFiles())
                iFileCount++;
            iFileCount++; // increment for this directory

            return iFileCount;
        }

        public void RenameFile(string strOldFileNameFull, string newFileName)
        {
            string strBaseDir = strOldFileNameFull.Substring(0, strOldFileNameFull.LastIndexOf('\\') +1);
            System.IO.File.Move(strOldFileNameFull, strBaseDir + newFileName);
        }

        public string NewFile(string strNewFileName)
        {
            EnsureFileDirectoryExists(strNewFileName);
            string strShortFileName = strNewFileName.Substring( strNewFileName.LastIndexOf('\\') + 1);
            string strFileSuffix = strShortFileName.Substring(strShortFileName.LastIndexOf('.') + 1);
            string strFileName = strShortFileName.Substring(0, strShortFileName.LastIndexOf('.'));
            string strDirName = strNewFileName.Substring(0, strNewFileName.LastIndexOf('\\') + 1);

            string strUniqueFileName = strNewFileName;
            bool bIsUnique = false;
            int i = 1;
            while (!bIsUnique)
            {
                if (!System.IO.File.Exists(strUniqueFileName))
                    bIsUnique = true;
                else
                {
                    strUniqueFileName = strDirName + strFileName + i.ToString() + "." + strFileSuffix;
                    i++;
                }
            }
            FileUtils.EnsureFileIsWriteable(strUniqueFileName);
            using (System.IO.StreamWriter oWriter = new System.IO.StreamWriter(strUniqueFileName))
            {
                oWriter.WriteLine(" ");
            }
            return strUniqueFileName;
        }

        public static void ParseProjectFileName(string projectFileName, out string projectName, out string baseDirectory)
        {
            projectName = "";
            baseDirectory = "";

            baseDirectory = projectFileName.Substring(0, projectFileName.LastIndexOf('\\'));
            projectName = projectFileName.Substring(projectFileName.LastIndexOf('\\') + 1);
            if (projectName.Contains(".dbsp"))
                projectName = projectName.Substring(0, projectName.IndexOf(".dbsp"));

        }
    }
}
